package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsPressMachineBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsPressMachineVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsPressMachine;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsPressMachineMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsPressMachineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 力学实验室压力机上传数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsPressMachineServiceImpl implements INLaboratoryMechanicsPressMachineService {

    private final NLaboratoryMechanicsPressMachineMapper baseMapper;

    /**
     * 查询力学实验室压力机上传数据
     */
    @Override
    public NLaboratoryMechanicsPressMachineVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询力学实验室压力机上传数据列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsPressMachineVo> queryPageList(NLaboratoryMechanicsPressMachineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsPressMachine> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsPressMachineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询力学实验室压力机上传数据列表
     */
    @Override
    public List<NLaboratoryMechanicsPressMachineVo> queryList(NLaboratoryMechanicsPressMachineBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsPressMachine> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsPressMachine> buildQueryWrapper(NLaboratoryMechanicsPressMachineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsPressMachine> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSampleKey()), NLaboratoryMechanicsPressMachine::getSampleKey, bo.getSampleKey());
        lqw.eq(StringUtils.isNotBlank(bo.getLingQi()), NLaboratoryMechanicsPressMachine::getLingQi, bo.getLingQi());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryMechanicsPressMachine::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getTestId()), NLaboratoryMechanicsPressMachine::getTestId, bo.getTestId());
        lqw.eq(StringUtils.isNotBlank(bo.getTester()), NLaboratoryMechanicsPressMachine::getTester, bo.getTester());
        lqw.eq(StringUtils.isNotBlank(bo.getQddj()), NLaboratoryMechanicsPressMachine::getQddj, bo.getQddj());
        lqw.eq(StringUtils.isNotBlank(bo.getSize()), NLaboratoryMechanicsPressMachine::getSize, bo.getSize());
        lqw.eq(StringUtils.isNotBlank(bo.getXs()), NLaboratoryMechanicsPressMachine::getXs, bo.getXs());
        lqw.eq(StringUtils.isNotBlank(bo.getYxlz()), NLaboratoryMechanicsPressMachine::getYxlz, bo.getYxlz());
        lqw.eq(StringUtils.isNotBlank(bo.getYxqd()), NLaboratoryMechanicsPressMachine::getYxqd, bo.getYxqd());
        lqw.eq(StringUtils.isNotBlank(bo.getPdjg()), NLaboratoryMechanicsPressMachine::getPdjg, bo.getPdjg());
        lqw.eq(bo.getCreateDate() != null, NLaboratoryMechanicsPressMachine::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCreator()), NLaboratoryMechanicsPressMachine::getCreator, bo.getCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getShape()), NLaboratoryMechanicsPressMachine::getShape, bo.getShape());
        lqw.eq(StringUtils.isNotBlank(bo.getLength()), NLaboratoryMechanicsPressMachine::getLength, bo.getLength());
        lqw.eq(StringUtils.isNotBlank(bo.getWidth()), NLaboratoryMechanicsPressMachine::getWidth, bo.getWidth());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), NLaboratoryMechanicsPressMachine::getHeight, bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getDiameter()), NLaboratoryMechanicsPressMachine::getDiameter, bo.getDiameter());
        lqw.eq(StringUtils.isNotBlank(bo.getSpan()), NLaboratoryMechanicsPressMachine::getSpan, bo.getSpan());
        lqw.eq(StringUtils.isNotBlank(bo.getSample()), NLaboratoryMechanicsPressMachine::getSample, bo.getSample());
        lqw.eq(StringUtils.isNotBlank(bo.getPinZhong()), NLaboratoryMechanicsPressMachine::getPinZhong, bo.getPinZhong());
        lqw.eq(StringUtils.isNotBlank(bo.getK()), NLaboratoryMechanicsPressMachine::getK, bo.getK());
        return lqw;
    }

    /**
     * 新增力学实验室压力机上传数据
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsPressMachineBo bo) {
        NLaboratoryMechanicsPressMachine add = BeanUtil.toBean(bo, NLaboratoryMechanicsPressMachine.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改力学实验室压力机上传数据
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsPressMachineBo bo) {
        NLaboratoryMechanicsPressMachine update = BeanUtil.toBean(bo, NLaboratoryMechanicsPressMachine.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsPressMachine entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除力学实验室压力机上传数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 压力机设备列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsPressMachineVo> page(NLaboratoryMechanicsPressMachineBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<NLaboratoryMechanicsPressMachine> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsPressMachineVo> result = baseMapper.page(pageQuery.build(), bo);
        // 获取主表id
        List<NLaboratoryMechanicsPressMachineVo> records = result.getRecords();
        List<String> ids = records.stream().map(NLaboratoryMechanicsPressMachineVo::getId).collect(Collectors.toList());
        // 获取试样数量
        if (CollectionUtils.isNotEmpty(ids)) {
            List<NLaboratoryMechanicsPressMachineVo> details = baseMapper.getDetailInfo(ids);
            // 转map合并对象信息
            Map<String, NLaboratoryMechanicsPressMachineVo> map =
                details.stream().collect(Collectors.toMap(NLaboratoryMechanicsPressMachineVo::getId, a -> a));
            for (NLaboratoryMechanicsPressMachineVo record : records) {
                if (map.containsKey(record.getId())) {
                    record.setSampleNum(map.get(record.getId()).getSampleNum());
                }
            }
            result.setRecords(records);
        }
        return TableDataInfo.build(result);
    }
}
